package com.adjy.pathbuilders.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.adjy.pathbuilders.world.Level;

public class GameDatabase {
	
	public static final String LEVEL_TABLE_NAME = "LEVELS";
	public static final String KEY_LEVEL_ID = "level_id"; // Primary Key
	public static final String KEY_LEVEL_COINS = "coins";
	public static final String KEY_LEVEL_TIME = "time";
	public static final String KEY_LEVEL_ENABLED = "enabled";
	
	public static final String AUDIO_TABLE_NAME = "AUDIO";
	public static final String KEY_AUDIO_VOLUME = "volume";
	public static final String KEY_AUDIO_MUTED = "mute";
	
	private static GameDatabase gameDatabase;
	
	private SQLiteDatabase db;
	private boolean started;
	
	private GameDatabase(Context c) {
		db = new GameDatabaseOpenHelper(c).getWritableDatabase();
		started = false;
	}
	
	public void setVolume(int volume, boolean isMuted) {
		db.delete(AUDIO_TABLE_NAME, null, null);
		ContentValues values = new ContentValues();
		values.put(KEY_AUDIO_VOLUME, volume);
		values.put(KEY_AUDIO_MUTED, isMuted ? 1 : 0);
		db.insertWithOnConflict(AUDIO_TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
	}
	
	public int getVolume() {
		Cursor c = db.query(false, 
		        AUDIO_TABLE_NAME,                        /**< Table name. */
		        null,                             /**< All the fields that you want the 
		                                                cursor to contain; null means all.*/
		        null,              /**< WHERE statement without the WHERE clause. */
		        null,    /**< Selection arguments. */
		        null, null, null, null);
		
		if (c.getCount() > 0) {

			c.moveToFirst();
			
			int volume = c.getInt(c.getColumnIndexOrThrow(KEY_AUDIO_VOLUME));
			
			c.close();
			
			return volume;
		}
		
		return 100;
	}
	
	public boolean getMuted() {
		Cursor c = db.query(false, 
		        AUDIO_TABLE_NAME,                        /**< Table name. */
		        null,                             /**< All the fields that you want the 
		                                                cursor to contain; null means all.*/
		        null,              /**< WHERE statement without the WHERE clause. */
		        null,    /**< Selection arguments. */
		        null, null, null, null);
		
		if (c.getCount() > 0) {

			c.moveToFirst();
			
			int isMuted = c.getInt(c.getColumnIndexOrThrow(KEY_AUDIO_MUTED));
			
			c.close();
			
			return isMuted != 0; // False == 0
		}
		
		return false;
	}
	
	public void setLevelInformation(Level l) {
		String id = l.getCategory().toString() + "_" + l.getLevelID();
		ContentValues values = new ContentValues();
		values.put(KEY_LEVEL_ID, id);
		values.put(KEY_LEVEL_ENABLED, l.isEnabled());
		values.put(KEY_LEVEL_COINS, l.getMostCoins());
		values.put(KEY_LEVEL_TIME, l.getBestTime());
		db.insertWithOnConflict(LEVEL_TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
	}
	
	public void updateLevelFromBD(Level level) { // FIXME: check better way of doing this
		Cursor c = db.query(false, 
		        LEVEL_TABLE_NAME,                        /**< Table name. */
		        null,                             /**< All the fields that you want the 
		                                                cursor to contain; null means all.*/
		        KEY_LEVEL_ID + "=?",              /**< WHERE statement without the WHERE clause. */
		        new String[] { level.getCategory().toString() + "_" + level.getLevelID() },    /**< Selection arguments. */
		        null, null, null, null);
		
		int coins = 0;
		int time = -1;
		boolean enabled = false;
		
		if (c.getCount() > 0) {

			c.moveToFirst();
			
			coins = c.getInt(c.getColumnIndexOrThrow(KEY_LEVEL_COINS));
			time = c.getInt(c.getColumnIndexOrThrow(KEY_LEVEL_TIME));
			enabled = c.getInt(c.getColumnIndexOrThrow(KEY_LEVEL_ENABLED)) == 1;
			
			c.close();
		}

		level.setMostCoins(coins);
		level.setBestTime(time);
		if(enabled) {
			level.enable();
		}
	}
	
	
	public static void createDatabase(Context c) {
		gameDatabase = new GameDatabase(c);
	}
	
	public static GameDatabase getInstance() {
		return gameDatabase;
	}
	
	public void setStarted(boolean start){
		this.started = start;
	}
	
	public boolean getStarted(){
		return this.started;
	}
}
